import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';

import { AppController } from './app.controller';

import { AppService } from './app.service';

import { CatsModule } from './cats/cats.module';

import { AuthModule } from './auth/auth.module';

import { TypeOrmModule } from '@nestjs/typeorm';

import { RoleGuard } from './role/role.guard';

import { MsbmanageModule } from './msbmanage/msbmanage.module';

import { UsersModule } from './users/users.module';

import { SupplierModule } from './supplier/supplier.module';

import { QuoteModule } from './quote/quote.module';

import { ProductInfoModule } from './product-info/product-info.module';

import { ResponseService } from './response/response.service';

import { ResponseModule } from './response/response.module';

import { CustomerModule } from './customer/customer.module';

import { MadeModule } from './made/made.module';

import { CustomerSimpleManageModule } from './customer-simple-manage/customer-simple-manage.module';

import { CustomerSimpleChildModule } from './customer-simple-child/customer-simple-child.module';

import { WebsocketModule } from './websocket/websocket.module';

import { TodoModule } from './todo/todo.module';
import { CustomerQuotationModule } from './customer-quotation/customer-quotation.module';
import { UploadModule } from './common/upload/upload.module';

import * as session from 'express-session';

import * as passport from 'passport';

@Module({
  imports: [
    CatsModule,
    AuthModule,
    TypeOrmModule.forRoot({
      type: 'mssql', // 数据库类型
      host: '120.26.246.60', // 主机
      port: 1433, // 端口
      username: 'sa', // 用户名
      password: 'Dmz081027', // 密码
      database: 'LtSystemManageDataSource', // 数据库名
      entities: [__dirname + '/**/*.entity{.ts,.js}'], // 实体文件
      synchronize: false, // 是否自动同步实体类到数据库
      retryDelay: 500, // 重试连接数据库间隔
      retryAttempts: 10, // 重试连接数据库的次数
      autoLoadEntities: true, // 是否自动加载实体
      options: {
        encrypt: false, // 是否加密连接（根据需要设置）
      },
      extra: {
        trustServerCertificate: true, // 如果连接到本地或自签名证书服务器，需要设置为 true
      },
    }),
    MsbmanageModule,
    UsersModule,
    SupplierModule,
    QuoteModule,
    ProductInfoModule,
    ResponseModule,
    CustomerModule,
    MadeModule,
    CustomerSimpleManageModule,
    CustomerSimpleChildModule,
    WebsocketModule,
    TodoModule,
    CustomerQuotationModule,
    UploadModule,
  ],
  controllers: [AppController],
  providers: [AppService, RoleGuard, ResponseService],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(
        session({
          secret: 'asdsaeiur8212!;12812', // 请使用一个更安全的密钥
          resave: false,
          saveUninitialized: false,
          cookie: { maxAge: 86400000 }, // 设置会话过期时间为24小时 (24 * 60 * 60 * 1000)
        }),
        passport.initialize(),
        passport.session(),
      )
      .forRoutes('*');
  }
}
